#!/bin/bash

#echo "$0" >> /tmp/foo
DIR=`dirname  "$0"`
CUR=`basename "$0"`
KEY=`echo $CUR|sed 's/_.*$//'`

if [ `echo -n "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" |wc -l` = 2 ] && [ $CUR = 'addtoMeld.sh' ] ; then
    FIL=`echo "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS"|head -1`
    NEW=`echo "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS"|head -2|tail -1`
    meld "$FIL" "$NEW" &
#    echo meld "$FIL" "$NEW" >> /tmp/foo
    exit 0
fi

if [ `echo -n "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" |wc -l` = 1 ] && [ $CUR = 'addtoMeld.sh' ] ; then
    FIL=`echo -n $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS|sed 's:/:／:g'`
    TMP=compareWith_$FIL
    rm -f ${DIR}/compareWith_*
    ln -s ${DIR}/$CUR "${DIR}/$TMP"
#    echo ln -s ${DIR}/$CUR "${DIR}/$TMP" >> /tmp/foo
    exit 0
fi


if [ `echo -n "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" |wc -l` = 1 ] && [ $KEY = 'compareWith' ] ; then
    FIL="`echo $CUR|sed 's/^compareWith_//'|sed 's:／:/:g'`"
    NEW="`echo -n $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS`"
    meld "$FIL" "$NEW" &
#    echo meld "$FIL" "$NEW" >> /tmp/foo
    rm -f "$0"
    exit 0
fi

# 支持文件名中含有空格，但是不要连续两个及以上空格;
# 文件名中不得有全角斜线／;

